package com.spgf.controller;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Toast;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.utils.AndroidUtil;

public abstract class PadraoActivity extends Activity implements Button.OnClickListener, OnItemLongClickListener, OnItemSelectedListener {

	protected void configurarEditar() throws NegocioException {}

	protected void construirTela(Bundle savedInstanceState) throws NegocioException {}

	protected void executarClick(View pView) throws NegocioException {}

	protected boolean executarLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {
		return false;
	}

	protected void executarSelecao(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {}

	@Override
	public void onClick(final View pView) {

		try {
			executarClick(pView);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), AndroidUtil.obterMensagemErro(e), Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			construirTela(savedInstanceState);
			configurarEditar();
		} catch (NegocioException e) {
			Toast.makeText(this, AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) {
		try {
			return executarLongClick(pParent, pView, pPosition, pID);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), e.getMensagemErro().toString(), Toast.LENGTH_SHORT).show();
			return false;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> pParent, View pView, int pPosition, long pID) {
		try {
			executarSelecao(pParent, pView, pPosition, pID);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), e.getMensagemErro().toString(), Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {}

	@Override
	protected void onRestart() {
		super.onRestart();

		try {
			atualizarLista();
		} catch (NegocioException e) {
			Toast.makeText(this, AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}
	}

	public void atualizarLista() throws NegocioException {}
}
